package crazyboyfeng.autodroid.action;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.provider.Settings;
import android.view.WindowManager;

import crazyboyfeng.autodroid.bean.TaskBean;
import crazyboyfeng.autodroid.bean.TaskTypeBean;
import crazyboyfeng.autodroid.utils.GlobalContext;

public class TaskTypeAction {

	GlobalContext context = GlobalContext.getInstance();

	public void doTaskType(TaskBean task) throws Exception {
		String mType = task.getTask();
		if (mType.equals(TaskTypeBean.FLIGHT)) {
			actionFLIGHT(task);
		} else if (mType.equals(TaskTypeBean.DATA)) {
			actionDATA(task);
		} else if (mType.equals(TaskTypeBean.WLAN)) {
			actionWLAN(task);
		} else if (mType.equals(TaskTypeBean.SOUND)) {
			actionSOUND(task);
		} else if (mType.equals(TaskTypeBean.QUAKE)) {
			actionQUAKE(task);
		} else if (mType.equals(TaskTypeBean.BLUETOOTH)) {
			actionBLUETOOTH(task);
		} else if (mType.equals(TaskTypeBean.GPS)) {
			actionGPS(task);
		}else if (mType.equals(TaskTypeBean.SYNC)) {
			actionSYNC(task);
		}else if (mType.equals(TaskTypeBean.LIGHT)) {
			actionLIGHT(task);
		}else if (mType.equals(TaskTypeBean.DARK)) {
			actionDARK(task);
		}else if (mType.equals(TaskTypeBean.ROTATE)) {
			actionROTATE(task);
		}else if (mType.equals(TaskTypeBean.LOCK)) {
			actionLOCK(task);
		}
	}

	private void actionLOCK(TaskBean task) {
		PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
		WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
		KeyguardManager  km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
		KeyguardLock kl = km.newKeyguardLock("unLock");  
if(task.getState().equals("Y")){
kl.reenableKeyguard();
}else{
	wl.acquire();
	kl.disableKeyguard();
	kl.reenableKeyguard();
	wl.release();
}
	}
	
	private void actionROTATE(TaskBean task) {
		Settings.System.putInt(context.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION,task.getState().equals("Y")?1:0);
	}

	private void actionDARK(TaskBean task) {
		ContentResolver cr = context.getContentResolver();
		Uri uri =Settings.System.getUriFor("screen_brightness");
		if(task.getState().equals("Y")){
			Settings.System.putInt(cr,
	                Settings.System.SCREEN_BRIGHTNESS_MODE,
	                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
	        Settings.System.putInt(cr, "screen_brightness",1);
		}else{
			Settings.System.putInt(cr,Settings.System.SCREEN_BRIGHTNESS_MODE,Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
		}
        cr.notifyChange(uri, null);
	}

	private void actionLIGHT(TaskBean task) {
		ContentResolver cr = context.getContentResolver();
		Uri uri = Settings.System.getUriFor("screen_brightness");
		if(task.getState().equals("Y")){
			Settings.System.putInt(cr,
	                Settings.System.SCREEN_BRIGHTNESS_MODE,
	                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
	        Settings.System.putInt(cr, "screen_brightness",255);
		}else{
			Settings.System.putInt(cr,Settings.System.SCREEN_BRIGHTNESS_MODE,Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
		}
        cr.notifyChange(uri, null);
	}

	private void actionSYNC(TaskBean task) {
		ContentResolver cr = context.getContentResolver();
		cr.setMasterSyncAutomatically(task.getState().equals("Y"));
	}

	private void actionGPS(TaskBean task) {
		LocationManager alm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER) != task
				.getState().equals("Y")) {
			Intent intent = new Intent();
			intent.setClassName("com.android.settings",
					"com.android.settings.widget.SettingsAppWidgetProvider");
			intent.addCategory("android.intent.category.ALTERNATIVE");
			intent.setData(Uri.parse("custom:3"));
			context.sendBroadcast(intent);
		}
	}

	private void actionBLUETOOTH(TaskBean task) {
		BluetoothAdapter bluetoothadapter = BluetoothAdapter
				.getDefaultAdapter();
		if (task.getState().equals("Y")) {
			bluetoothadapter.enable();
		} else {
			bluetoothadapter.disable();
		}
	}

	private void actionQUAKE(TaskBean task) {
		AudioManager audioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		if (task.getState().equals("Y")) {
			audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
		} else {
			audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		}
	}
	private void actionSOUND(TaskBean task) {
		AudioManager audioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		if (task.getState().equals("Y")) {
			audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		} else {
			audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		}
	}
	private void actionWLAN(TaskBean task) {// wlan
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		if (task.getState().equals("Y")) {
			wifiManager.setWifiEnabled(true);
		} else {
			wifiManager.setWifiEnabled(false);
		}
	}
	private void actionDATA(TaskBean task) throws Exception {// 流量
		final ConnectivityManager conman = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		final Class conmanClass = Class.forName(conman.getClass().getName());
		final Field iConnectivityManagerField = conmanClass
				.getDeclaredField("mService");
		iConnectivityManagerField.setAccessible(true);
		final Object iConnectivityManager = iConnectivityManagerField
				.get(conman);
		final Class iConnectivityManagerClass = Class
				.forName(iConnectivityManager.getClass().getName());
		final Method setMobileDataEnabledMethod = iConnectivityManagerClass
				.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
		setMobileDataEnabledMethod.setAccessible(true);
		if (task.getState().equals("Y")) {
			setMobileDataEnabledMethod.invoke(iConnectivityManager, true);
		} else {
			setMobileDataEnabledMethod.invoke(iConnectivityManager, false);
		}

	}
	private void actionFLIGHT(TaskBean task) {// 飞行模式
		ContentResolver cr = context.getContentResolver();
		String air_state = ""; // 0为关闭，1为开启
		// 判断任务状态为开启
		if (task.getState().equals("Y")) {
			air_state = "1";
		} else {
			air_state = "0";
		}
		Settings.System.putString(cr, Settings.System.AIRPLANE_MODE_ON,
				air_state);
		Intent mIntent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
		context.sendBroadcast(mIntent);
	}
}
